package com.woniu.daiquhua.config;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class ShiroConfiguration {

    /**
     * 密码校验规则HashedCredentialsMatcher
     * 这个类是为了对密码进行编码的 ,
     * 防止密码在数据库里明码保存 , 当然在登陆认证的时候 ,
     * 这个类也负责对form里输入的密码进行编码
     * 处理认证匹配处理器：如果自定义需要实现继承HashedCredentialsMatcher
     */
    @Bean("hashedCredentialsMatcher")
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        //指定加密方式为MD5
        credentialsMatcher.setHashAlgorithmName("MD5");
        //加密次数 默认1235
        credentialsMatcher.setHashIterations(1235);
        credentialsMatcher.setStoredCredentialsHexEncoded(true);
        return credentialsMatcher;
    }



//    @Bean("authRealm")
//    @DependsOn("lifecycleBeanPostProcessor")//可选
//    public AuthRealm authRealm(@Qualifier("hashedCredentialsMatcher") HashedCredentialsMatcher matcher) {
//        AuthRealm authRealm = new AuthRealm();
//        authRealm.setAuthorizationCachingEnabled(false);
//        authRealm.setCredentialsMatcher(matcher);
//        return authRealm;
//    }
//
//
//    /**
//     * 定义安全管理器securityManager,注入自定义的realm
//     * @param authRealm
//     * @return
//     */
//    @Bean("securityManager")
//    public SecurityManager securityManager(@Qualifier("authRealm") AuthRealm authRealm) {
//        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
//        manager.setRealm(authRealm);
//        return manager;
//    }
//
//
//    /**
//     * 定义shiroFilter过滤器并注入securityManager
//     * @param manager
//     * @return
//     */
//    @Bean("shiroFilter")
//    public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") SecurityManager manager) {
//        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
//        //设置securityManager
//        bean.setSecurityManager(manager);
//        //设置登录页面
//        //可以写路由也可以写jsp页面的访问路径
//        bean.setLoginUrl("/login");
//        //设置登录成功跳转的页面
//        bean.setSuccessUrl("/pages/index.jsp");
//        //设置未授权跳转的页面
//        bean.setUnauthorizedUrl("/pages/unauthorized.jsp");
//        //定义过滤器
//        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
//        filterChainDefinitionMap.put("/index", "authc");
//        filterChainDefinitionMap.put("/login", "anon");
//        filterChainDefinitionMap.put("/loginUser", "anon");
//        filterChainDefinitionMap.put("/admin", "roles[admin]");
//        filterChainDefinitionMap.put("/edit", "perms[delete]");
//        filterChainDefinitionMap.put("/druid/**", "anon");
//        //需要登录访问的资源 , 一般将/**放在最下边
//        filterChainDefinitionMap.put("/**", "authc");
//        bean.setFilterChainDefinitionMap(filterChainDefinitionMap);
//        return bean;
//    }
//
//    /**
//     * Spring的一个bean , 由Advisor决定对哪些类的方法进行AOP代理 .
//     * @return
//     */
//    @Bean
//    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
//        DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
//        creator.setProxyTargetClass(true);
//        return creator;
//    }
//
//    /**
//     * 配置shiro跟spring的关联
//     * @param securityManager
//     * @return
//     */
//    @Bean
//    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") SecurityManager securityManager) {
//        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
//        advisor.setSecurityManager(securityManager);
//        return advisor;
//    }
//
//    /**
//     * lifecycleBeanPostProcessor是负责生命周期的 , 初始化和销毁的类
//     * (可选)
//     */
//    @Bean("lifecycleBeanPostProcessor")
//    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
//        return new LifecycleBeanPostProcessor();
//    }

}
